package com.neu.chenzhiping.Servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.neu.chenzhiping.Service.StuService1;
import com.neu.chenzhiping.lei.*;
import com.neu.commons.Utils.Excelutil;

@WebServlet(name = "ControllerServlet", urlPatterns = {"*.do"})
@MultipartConfig
public class ControllerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String uri=request.getRequestURI();
        String u=uri.substring(uri.lastIndexOf("/")+1);
        Map<String,String[]>map=request.getParameterMap();
        HttpSession session=request.getSession();
        switch (u){

            case "ToadddorServlet.do":
                request.getRequestDispatcher("WEB-INF/Mgr/add_dor.jsp").forward(request,response);
                break;

            case "ToCheckDorServlet.do":
               String pageIndex=request.getParameter("pageIndex");
                dor d=new dor();
               String pageNum=d.getpageNum(6);

                List<Map<String, Object>>list=d.findBypage(pageIndex,6);

                session.setAttribute("pageIndex",pageIndex);
                session.setAttribute("pageNum",pageNum);
               session.setAttribute("list",list);
     request.getRequestDispatcher("WEB-INF/Mgr/CheckDor.jsp").forward(request, response);

                break;
            case "adddorServlet.do":
                dor a=new dor();
                boolean flag =a.tianjia(map);
                if (flag){
                   response.sendRedirect(request.getContextPath()+"/commons/success.jsp");
                }else {
                    response.sendRedirect(request.getContextPath()+"/commons/fail.jsp");
                }
                break;
                case "SingleCheckServlet.do":
                    Map<String,Object>item=new HashMap<>();
                    dor r=new dor();
                       item=r.getSingle(map);
                    System.out.println(item);
                    session.setAttribute("it",item);
                    request.getRequestDispatcher("WEB-INF/Mgr/xianshi.jsp").forward(request,response);

                    break;
            case "DeleteDorServlet.do":
                System.out.println(map);
                dor de=new dor();
                 boolean f= de.deletedor(map);
                System.out.println(f);
                if (f){
                response.sendRedirect(request.getContextPath()+"/commons/success.jsp");
            }
                break;
            case "TofenpeiServlet.do":
                request.getRequestDispatcher("WEB-INF/Mgr/fenpei.jsp").forward(request,response);
                break;
            case "FileUpServlet.do":
                Part part=request.getPart("wj");
                InputStream in=part.getInputStream();
                    List<Map<String,String>>list1=Excelutil.readExcel(in);
                System.out.println(list1);
                 StuService1 st=new StuService1();
               boolean fg = st.doSave(list1);
               if (fg){
                   response.sendRedirect(request.getContextPath()+"/commons/success.jsp");
               }
                break;
            case "zhuServlet.do":
                request.getRequestDispatcher("WEB-INF/Mgr/mgr_main.jsp").forward(request,response);
                break;
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
